<?php
// +----------------------------------------------------------------------
// | HisiPHP框架[基于ThinkPHP5.1开发]
// +----------------------------------------------------------------------
// | Copyright (c) 2016-2021 http://www.HisiPHP.com
// +----------------------------------------------------------------------
// | HisiPHP承诺基础框架永久免费开源，您可用于学习和商用，但必须保留软件版权信息。
// +----------------------------------------------------------------------
// | Author: 橘子俊 <364666827@qq.com>，开发者QQ群：50304283
// +----------------------------------------------------------------------

namespace app\common\service;

use app\common\service\Base;
use app\common\service\Order as OrderService;

class GeneralMaterial extends Base
{

	/**
	 * 检测物料名称是否已存在
	 *
	 * @author KING
	 * @date 2020年7月9日
	 *
	 * @param string $name
	 * @param integer $exceptId
	 * @return boolean
	 */
	public function checkNameExist($name, $exceptId=0) {
		$where = [];
		$where[] = ['name', '=', $name];
		if ($exceptId) {
			$where[] = ['id', '<>', $exceptId];
		}
		$info = db('general_material')->where($where)->field('id')->findOrEmpty();
		return $info?true:false;
	}

	/**
	 * 根据物料sku获取通用料库存信息
	 *
	 * @author KING
	 * @date 2020年7月22日
	 *
	 * @param string $materialSku
	 * @param string $field
	 * @return array
	 */
	public function getInfoByMaterialSku($materialSku, $field='*') {
		return db('general_material')->where('id', 'in', db('general_material_sku')->where('sku', $materialSku)->column('general_material_id'))->field($field)->findOrEmpty();
	}

	/**
	 * 检测物料sku是否已存在
	 *
	 * @author KING
	 * @date 2020年7月9日
	 *
	 * @param string $sku
	 * @param integer $exceptId
	 * @return boolean
	 */
	public function checkSkuExist($sku, $exceptId=0) {
		$where = [];
		$where[] = ['sku', '=', $sku];
		if ($exceptId) {
			$where[] = ['general_material_id', '!=', $exceptId];
		}
		$info = db('general_material_sku')->where($where)->field('id')->findOrEmpty();
		return $info?true:false;
	}

	/**
	 * 转换通用料库存为辅料库存
	 *
	 * @author King
	 * @date 2020年10月10日
	 *
	 * @param integer $materialId	物料ID
	 * @param integer $orderId		订单ID
	 * @param string $materialSku	物料SKU
	 * @param number $stockNum		转换量
	 */
	public function changeToStock($materialId, $orderId, $materialSku, $stockNum) {
		$list = db('general_material')->where('id', 'in', db('general_material_sku')->where('sku', $materialSku)->column('general_material_id'))->where('stock', '>', 0)->order('id asc')->select();
		if(empty($list)) return true;
		$afterSubNum = $stockNum;

		// 通用料扣除
		foreach ($list as $k => $v) {
			if ($afterSubNum == 0) break;
			$subNum = min([$afterSubNum, $v['stock']]);
			db('general_material')->where('id', '=', $v['id'])->setDec('stock', $subNum);

			$afterSubNum = $afterSubNum - $subNum;
		}
		$changeNum = round($stockNum - $afterSubNum, 2); // 最终转换的量
		if ($changeNum <= 0) return true;

		// 辅料入库
		$stockInfo = db('stock')->where('material_id', $materialId)->findOrEmpty();
		if (!$stockInfo) {
			// 初始化记录
			$stockInfo = [
				'material_id' => $materialId,
				'stock' => 0,
				'unit' => '',
				'create_time' => time(),
				'update_time' => time(),
			];
			$stockId = db('stock')->insertGetId($stockInfo);
		} else {
			$stockId = $stockInfo['id'];
		}

		$oInfo = (new OrderService())->getInfoById($orderId);

		// 记录入库日志
		$stockLogData = [
			'type' => 1,
			'stock_id' => $stockId,
			'material_id' => $materialId,
			'factory_id' => $oInfo['factory_id'],
			'order_id' => $orderId,
			'client_ordernum' => $oInfo['client_ordernum'],
			'sku' => $oInfo['sku'],
			'use_from_id' => 0,
			'left_num' => $stockInfo['stock']+$changeNum,
			'operate_num' => $changeNum,
			'remark' => '通用库存转入',
			'create_time' => time(),
			'update_time' => time(),
		];
		db('stock_log')->insertGetId($stockLogData);

		db('stock')->where('id', '=', $stockId)->setInc('stock', $changeNum);

		return true;
	}
}